/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.actions;

import com.google.inject.Inject;
import com.google.inject.Provider;
import listo.client.*;
import listo.client.dialogs.AddTaskDialog;
import listo.client.dialogs.TaskDialog;
import listo.client.model.Folder;
import listo.client.model.Task;
import listo.client.model.operations.AddTaskOp;
import listo.client.viewlists.FolderView;
import listo.client.viewlists.FolderViewList;
import listo.utils.guice.CustomScopes;
import listo.utils.logging.Log;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class AddTaskAction extends BaseAction implements TaskDialog.OkCancelListener<Task> {

    private final ContextManager contextManager;
    private final Provider<AddTaskDialog> taskFormProvider;
    private final FolderViewList folderViewList;
    private final Preferences preferences;
    private final MainForm mainForm;
    private AddTaskDialog dialog;

    @Inject
    protected AddTaskAction(Log log, Lang lang, ImageProvider imageProvider, ContextManager contextManager,
                            Provider<AddTaskDialog> taskFormProvider,
                            FolderViewList folderViewList, Preferences preferences, MainForm mainForm) {
        super(log, lang, "addTask");
        this.contextManager = contextManager;
        this.taskFormProvider = taskFormProvider;
        this.folderViewList = folderViewList;
        this.preferences = preferences;
        this.mainForm = mainForm;

        putValue(Action.SMALL_ICON, imageProvider.getIcon("16x16/task_add.png"));
        putValue(Action.LARGE_ICON_KEY, imageProvider.getIcon("22x22/task_add.png"));

        putValue(Action.ACCELERATOR_KEY, preferences.getNewTaskHotKey());
    }

    /**
     * Invoked when an action occurs.
     */
    public void perform(ActionEvent e) {
        if (dialog != null) {
            // if we the dialog is currently open just bring it forward
            dialog.toFront();
            return;
        }

        dialog = taskFormProvider.get();
        dialog.setTitle("Add new task");
        dialog.setOkListener(this);

        Task task = new Task();
        task.setDesc("Do something");

        FolderView[] selectedViews = Convert.toFolderViews(folderViewList.getSelectedValues());
        if (mainForm.isVisible() && selectedViews.length > 0) {
            task.setFolderIds(Convert.toFolderIds(selectedViews));
        } else {
            Folder defaultFolder = contextManager.getCurrentContext()
                    .getFolderByPathName(preferences.getDefaultFolder());
            if (defaultFolder != null) task.setFolderIds(defaultFolder.getId());
        }

        dialog.run(task);
    }

    public void onOk(Task task) {
        boolean addAnother = dialog.isOkAndNewPressed();
        onCancel(task);

        AddTaskOp op = new AddTaskOp();

        op.addField("desc", task.getDesc());
        if (task.getDue() != null) op.addField("due", task.getDue());
        if (task.hasReminder()) op.addField("reminder", task.getReminder().toString());
        if (task.hasFolders()) op.addField("folders", task.getFolderIdsString());
        if (task.getNote() != null) op.addField("note", task.getNote());

        contextManager.addAndRun(op);

        if (addAnother) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    perform(null);
                }
            });
        }
    }

    public void onCancel(Task task) {
        dialog = null;
        CustomScopes.MANUAL.reset("TaskDialogScope");
    }

}